package com.javaclimb.music.datastructure;

public class Stack {
    private  int maxSize;
    private  int[] arr;
    private  int top;

    public Stack(int maxSize) {
        this.maxSize = maxSize;
        this.arr=new int[maxSize];
        top=-1;
    }

    public void push(int num){
            try {
                top++;
                arr[top] = num;
            }catch (Exception e){
                System.out.println("栈已满，无法添加");
            }
    }

    public void getTop(){
        System.out.println(arr[top]);
    }

    public int pop(){
        return arr[top--];
    }

    public void display(){
        for (int i=top;i>=0;i--){
            if (i==0){
                System.out.println(arr[i]+"  ");
            }else {
                System.out.print(arr[i]+"  ");
            }
        }
    }

    public static void main(String[] args) {
        Stack s=new Stack(5);
        s.push(3);
        s.push(5);
        s.push(1);
        s.getTop();
        s.display();
        s.pop();
        s.getTop();
        s.display();
        s.push(7);
        s.push(2);
        s.display();
        s.push(12);
        s.display();
        s.push(4);

    }
}
